﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using Lbb.Localization;
using Lbb.URating.DL;
using Lbb.URating.Localization;
using Lbb.Data;

namespace HU8.UCenter.Plugins.URating.DAL
{
    /// <summary>
    /// 用户评分分类操作库
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickCategoryRepository : RepositoryBase<U_ClickCategory>
    {
        #region Ctros
        /// <summary>
        /// 构建用户评分分类操作库
        /// </summary>
        /// <param name="db">数据源</param>
        public U_ClickCategoryRepository(URatingDataContext db)
            : base(db)
        {
        }
        #endregion Ctros

        #region Get
        /// <summary>
        /// 取得指定评分分类
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        public U_ClickCategory Get(Guid? id)
        {
            return id.HasValue && id != Guid.Empty ? Get(d => d.CategoryID == id) : null;
        }
        #endregion Get

        #region Find
        /// <summary>
        /// 搜索评分分类
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="objTypes">关联的对象类型</param>
        /// <param name="allowRedo">允许重新评分</param>
        /// <returns></returns>
        public IQueryable<U_ClickCategory> Find(string keyword = null, IEnumerable<string> objTypes = null, bool? allowRedo = null)
        {
            var data = GetAll();

            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(d => d.Title.Contains(keyword));
            }

            if (objTypes != null)
            {
                objTypes = objTypes.Where(d => !string.IsNullOrEmpty(d)).Select(d => d.ToUpper()).ToList();
                if (objTypes.Count() > 0)
                {
                    data = data.Where(d => objTypes.Contains(d.ObjectType.ToUpper()));
                }
            }

            if (allowRedo.HasValue)
            {
                data = data.Where(d => d.AllowRedo == allowRedo.Value);
            }

            return data;
        }
        #endregion Find


        #region Override
        /// <summary>
        /// 添加评分分类
        /// </summary>
        /// <param name="obj">评分分类</param>
        public override void Add(U_ClickCategory obj)
        {
            if (obj != null)
            {
                if (obj.CategoryID == Guid.Empty) obj.CategoryID = Guid.NewGuid();
                obj.ObjectType = obj.ObjectType.ToUpper();
            }
            base.Add(obj);
        }
        /// <summary>
        /// 更新评分分类
        /// </summary>
        /// <param name="obj">评分分类</param>
        public override void Update(U_ClickCategory obj)
        {
            CheckArg(obj);

            var old = Get(obj.CategoryID);
            if (old == null) throw new ArgumentNullException(string.Format(OperatorRes.NotFindEditingObj, U_ClickCategoryRes.ClassName));

            try
            {
                Table.Attach(obj, old);
            }
            catch (InvalidOperationException)
            {
                DB.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
            }
        }
        #endregion Override

    }
}
